package com.example.hrsystem.ui.attendance;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.model.DailyAttendanceRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 考勤详情适配器
 * 用于在RecyclerView中显示打卡记录列表
 */
public class AttendanceDetailAdapter extends RecyclerView.Adapter<AttendanceDetailAdapter.AttendanceViewHolder> {

    private List<DailyAttendanceRecord> attendanceRecordList;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());

    public AttendanceDetailAdapter(List<DailyAttendanceRecord> attendanceRecordList) {
        this.attendanceRecordList = attendanceRecordList;
    }

    @NonNull
    @Override
    public AttendanceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_attendance_detail, parent, false);
        return new AttendanceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AttendanceViewHolder holder, int position) {
        DailyAttendanceRecord record = attendanceRecordList.get(position);
        
        // 设置日期
        if (record.getDate() != null) {
            holder.dateTextView.setText(dateFormat.format(record.getDate()));
        }
        
        // 设置签到时间
        if (record.getCheckInTime() != null && !record.getCheckInTime().isEmpty()) {
            holder.checkInTimeTextView.setText(record.getCheckInTime());
        } else {
            holder.checkInTimeTextView.setText("未签到");
        }
        
        // 设置签退时间
        if (record.getCheckOutTime() != null && !record.getCheckOutTime().isEmpty()) {
            holder.checkOutTimeTextView.setText(record.getCheckOutTime());
        } else {
            holder.checkOutTimeTextView.setText("未签退");
        }
        
        // 设置打卡类型
        holder.attendanceTypeTextView.setText(record.getStatus());
        
        // 根据打卡类型设置颜色
        if ("正常".equals(record.getStatus())) {
            holder.attendanceTypeTextView.setTextColor(
                    holder.itemView.getContext().getResources().getColor(R.color.colorSuccess));
        } else {
            holder.attendanceTypeTextView.setTextColor(
                    holder.itemView.getContext().getResources().getColor(R.color.colorError));
        }
    }

    @Override
    public int getItemCount() {
        return attendanceRecordList.size();
    }

    /**
     * 考勤详情视图持有者
     */
    static class AttendanceViewHolder extends RecyclerView.ViewHolder {
        TextView dateTextView;
        TextView checkInTimeTextView;
        TextView checkOutTimeTextView;
        TextView attendanceTypeTextView;

        public AttendanceViewHolder(@NonNull View itemView) {
            super(itemView);
            dateTextView = itemView.findViewById(R.id.attendance_date_text_view);
            checkInTimeTextView = itemView.findViewById(R.id.check_in_time_text_view);
            checkOutTimeTextView = itemView.findViewById(R.id.check_out_time_text_view);
            attendanceTypeTextView = itemView.findViewById(R.id.attendance_type_text_view);
        }
    }
}